{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "邮件发送成功\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    " \n",
    "import smtplib\n",
    "from email.mime.text import MIMEText\n",
    "from email.mime.multipart import MIMEMultipart\n",
    "from email.header import Header\n",
    " \n",
    "sender = '1092017732@qq.com'\n",
    "receivers = ['1092017732@qq.com',\"954478212@qq.com\"]  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱\n",
    "   \n",
    "#创建一个带附件的实例\n",
    "message = MIMEMultipart()\n",
    "message['From'] = Header(\"python email\", 'utf-8')\n",
    "message['To'] =  Header(\"大刚妙优\", 'utf-8')\n",
    "subject = '芒果卡片card.db备份'\n",
    "message['Subject'] = Header(subject, 'utf-8')\n",
    " \n",
    "#邮件正文内容\n",
    "message.attach(MIMEText('这是菜鸟教程Python 邮件发送测试……', 'plain', 'utf-8'))\n",
    " \n",
    "# 构造附件1，传送当前目录下的 test.txt 文件\n",
    "att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')\n",
    "att1[\"Content-Type\"] = 'application/octet-stream'\n",
    "# 这里的filename可以任意写，写什么名字，邮件中显示什么名字\n",
    "att1[\"Content-Disposition\"] = 'attachment; filename=\"test.txt\"'\n",
    "message.attach(att1)\n",
    " \n",
    "try:\n",
    "    smtpObj = smtplib.SMTP_SSL('smtp.qq.com',465) \n",
    "    #smtpObj.login(sender,'NCJUNXDLGZRNCDRW') # mgangwork@163.com的pass\n",
    "    smtpObj.login(sender,'cirbkbzqieycicii') # 1092017732@qq.com的pass\n",
    "    smtpObj.sendmail(sender, receivers, message.as_string())\n",
    "    print(\"邮件发送成功\")\n",
    "except smtplib.SMTPException as result:\n",
    "    print(result)\n",
    "    print(\"Error: 无法发送邮件\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
